//
//  ViewController.m
//  108 - 特殊布局
//
//  Created by 董 尚先 on 15/3/20.
//  Copyright (c) 2015年 shangxianDante. All rights reserved.
//

#import "SXStackViewController.h"
#import "SXStackLayout.h"
#import "SXImageCell.h"

#import "ZJScreenAdaptaionMacro.h"
#import "ZJScreenAdaptation.h"

#import "UIKit+AFNetworking.h"
@interface SXStackViewController () <UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, weak) UICollectionView *collectionView;

@end

@implementation SXStackViewController

static NSString *const ID = @"image";

- (NSMutableArray *)images
{
    if (!_images) {
        self.images = [[NSMutableArray alloc] init];
        
//        for (int i = 1; i<=12; i++) {
//            [self.images addObject:[NSString stringWithFormat:@"%d", i]];
//        }
    }
    return _images;
}
+ (id)sXStack {
    return [[self alloc] init];
    
}
- (void)willMoveToSuperview:(UIView *)newSuperview {
    
    self.frame = CGRectMakeEx(0, 64, 375, 200);
//    self.backgroundColor = [UIColor blackColor];
    
    // 创建布局
    SXStackLayout *layout = [[SXStackLayout alloc] init];
    
    // 创建collectionView
    //重叠
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 200) collectionViewLayout:layout];
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.dataSource = self;
    collectionView.delegate = self;
    collectionView.backgroundColor = [UIColor clearColor];
    [collectionView registerNib:[UINib nibWithNibName:@"SXImageCell" bundle:nil] forCellWithReuseIdentifier:ID];
    [self addSubview:collectionView];
    self.collectionView = collectionView;
    
}

#pragma mark - <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.images.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SXImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    cell.image = self.images[indexPath.item];
    return cell;
}

#pragma mark - <UICollectionViewDelegate>
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    // 删除图片名
        [self.images removeObjectAtIndex:indexPath.item];
    
        // 刷新数据
        //    [self.collectionView reloadData];
    
        // 直接将cell删除
        [self.collectionView deleteItemsAtIndexPaths:@[indexPath]];
    NSLog(@"玄幻宗");
//    if (self.indexBlock) {
//        self.indexBlock(indexPath.item);
//    }
}


@end
